package cn.edu.swu.lzr.Book;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;


@WebServlet("/updateBook")
public class Updateservlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        int id = Integer.valueOf(request.getParameter("id"));
        String name = request.getParameter("name");
        String author = request.getParameter("author");
        Float price = Float.valueOf(request.getParameter("price"));
        Book book = new Book(id, name, author, price);

        try {
            BookRepo.getInstance().updateBook(book);
            response.sendRedirect("./main");
        } catch (SQLException | IOException e) {
            throw new ServletException(e);
        }
    }

}
